package com.thti.mecool.client.widget;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView;

import com.thti.mecool.client.activity.R;
import com.thti.mecool.client.activity.TaskActivity;
import com.thti.mecool.client.activity.TaskExecute;
import com.thti.mecool.client.adapter.MutilListAdapter;
import com.thti.mecool.client.util.ForbidGroupClickListener;

public class ListWidget {
	ExpandableListView TaskListView;
	Activity activity;
	ArrayList<ArrayList<HashMap<String, String>>> taskList;
	ArrayList<String> listTag;
	MutilListAdapter mutilAdapter;
//	MutilListAdapter SyncTaskAdapter;
//	ExpandableListView DoneTaskList;
	ArrayList<ArrayList<Integer>> stat;
	ExpandableListView taskundonelist;
	MutilListAdapter undone_detail_adapter;
	private ArrayList<String> curProjectIds,PlanIds,StoreNames,StoreCodes,taskIds,taskNames;
	int curGroup=0;
	int curChild=0;
	public ListWidget(Activity activity,
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag) {
		this.activity = activity;
		this.taskList = taskList;
		this.listTag=listTag;
		listinit();
	}
	public void setTaskListData(ArrayList<ArrayList<HashMap<String, String>>> taskList,ArrayList<String> listTag){
		this.taskList=taskList;
		this.listTag=listTag;
		mutilAdapter.notifyDataSetChanged();
	}
	public ListWidget(Activity activity,
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag,ArrayList<ArrayList<Integer>> stat) {
		this.activity = activity;
		this.taskList = taskList;
		this.listTag=listTag;
		this.stat=stat;
		listinit();
	}
	
	OnChildClickListener TaskListOnChildListener=new OnChildClickListener() {
		
		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			TextView Taskname=(TextView) v.findViewById(R.id.mecool_tasklist_itemname);
			curGroup=groupPosition;
			curChild=childPosition;
			Intent i=new Intent(activity,TaskExecute.class);
			i.putExtra("taskName", taskNames.get(groupPosition));
			i.putExtra("storeName", StoreNames.get(groupPosition));
			i.putExtra("storeCode", StoreCodes.get(groupPosition));
			i.putExtra("planId", PlanIds.get(groupPosition));
			i.putExtra("projectId", curProjectIds.get(groupPosition));
			i.putExtra("taskId", taskIds.get(groupPosition));
			activity.startActivity(i);
			return false;
		}
	};
	
	private void listinit() {
		if(activity.getClass().equals(TaskActivity.class)){
			TaskListView = (ExpandableListView) activity.findViewById(R.id.undone_task);
//			DoneTaskList=(ExpandableListView)activity.findViewById(R.id.mecool_done_task);
			mutilAdapter=new MutilListAdapter(taskList, listTag,activity);
//			SyncTaskAdapter=new MutilListAdapter(taskList, listTag, stat, activity);
			TaskListView.setAdapter(mutilAdapter);
//			DoneTaskList.setAdapter(SyncTaskAdapter);
			TaskListView.setOnChildClickListener(TaskListOnChildListener);
			listExtra(listTag);
			listRefurbish();
		}else{
			taskundonelist=(ExpandableListView)activity.findViewById(R.id.mecool_mutil_list);
			undone_detail_adapter=new MutilListAdapter(taskList, listTag, activity);
			taskundonelist.setAdapter(undone_detail_adapter);
			listExtra(listTag);
			OtherListRefurbish();
		}
	}
	public void setPlanIdAndProjectIds(ArrayList<String> planIds, ArrayList<String> projectIds, ArrayList<String> storeCodes, ArrayList<String> storeNames, ArrayList<String> taskIds,ArrayList<String> taskNames){
		this.taskNames=taskNames;
		this.PlanIds=planIds;
		this.curProjectIds=projectIds;
		this.StoreNames=storeNames;
		this.StoreCodes=storeCodes;
		this.taskIds=taskIds;
	}
	
	public void delData(int curPos){
		taskList.remove(curPos);
		listTag.remove(curPos);
		ArrayList<ArrayList<HashMap<String, String>>> TempArrList=new ArrayList<ArrayList<HashMap<String,String>>>();
		ArrayList<String> TempTagList=new ArrayList<String>();
		TempArrList.addAll(taskList);
		TempTagList.addAll(listTag);
		taskList.clear();
		listTag.clear();
		
		for(ArrayList<HashMap<String, String>> list:TempArrList){
			taskList.add(list);
		}
		for(String s : TempTagList){
			listTag.add(s);
		}
		OtherListRefurbish();
	}
	
	public ExpandableListView getotherlist(){
		return taskundonelist;
	}
	public void OtherListRefurbish(){
		undone_detail_adapter.notifyDataSetChanged();
	}
	public void listRefurbish(){
		if(mutilAdapter!=null)
		mutilAdapter.notifyDataSetChanged();
//		if(SyncTaskAdapter!=null)
//		SyncTaskAdapter.notifyDataSetChanged();
	}
	/**
	 * 本功能用于关闭收缩功能以及自动展开所有列表项
	 * 
	 * @param listTag
	 */
	public void listExtra(ArrayList<String> listTag) {
		if (!listTag.isEmpty()) {
			if(activity.getClass().equals(TaskActivity.class)){
			for (int i = 0; i < listTag.size(); i++) {
				TaskListView.expandGroup(i);
//				DoneTaskList.expandGroup(i);
			}
			TaskListView.setOnGroupClickListener(new ForbidGroupClickListener());
//			DoneTaskList.setOnGroupClickListener(new ForbidGroupClickListener());
			}else{
				for(int i=0;i<listTag.size();i++){
					taskundonelist.expandGroup(i);
				}
				taskundonelist.setOnGroupClickListener(new ForbidGroupClickListener());
			}
		}
	}
}
